package com.ecommerce.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ecommerce.dal.entity.Client;
import com.ecommerce.dal.entity.Company;
import com.ecommerce.dal.entity.Rank;
import com.ecommerce.dal.service.IClientManagerService;
import com.ecommerce.dal.service.ICompanyManagerService;
import com.ecommerce.dal.service.IRankManagerService;

@Controller
@RequestMapping("/clients.htm")
public class ClientController {

	@Autowired(required = true)
	private IClientManagerService clientManagerService;
	@Autowired(required = true)
	private ICompanyManagerService companyManagerService;
	@Autowired(required = true)
	private IRankManagerService rankManagerService;

	@RequestMapping(method=RequestMethod.GET)
	 public ModelAndView listClients(Map<String, Object> map) {
	
//	 Map<String, Object> model=new HashMap<String, Object>();
	 List<Client> clientList = clientManagerService.retrieveAllClients();
	 Client client = new Client();
	 map.put("clientList", clientList);
	 map.put("client", client);
//	 return new ModelAndView("test.jsp", map); //this first , compile , run , clean then next line!!!!
	 return new ModelAndView("client", map); // build from here
	 }

	@RequestMapping(value = "/save.htm", method = RequestMethod.POST)
	public ModelAndView saveClient(@ModelAttribute("client") Client client,
			BindingResult result) {
		
		client.setBirthDate(new Date());
		client.setJoinDate(new Date());
		client.setState(1);
		client.setSponsor(true);
		Company company = companyManagerService.retrieveCompany(1);
		Rank rank = rankManagerService.retrieveRank(1);
		client.setCompany(company);
		client.setRank(rank);
		clientManagerService.saveClient(client);

		return new ModelAndView("client");

	}

	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public ModelAndView addClient(@ModelAttribute("client") Client client,
			BindingResult result, ModelMap map) {
		map.put("client", client);
		System.out.println("This is just for testing ... ");
		Client client2 = clientManagerService.retrieveClient(3L);
		System.out.println("First Name: " + client2.getFirstName()
				+ "   , Last Name: " + client2.getLastName());
		return new ModelAndView("client", map);
	}

	@RequestMapping("/delete/{clientId}")
	public String deleteClient(@PathVariable("clientId") Long clientId) {

		clientManagerService.deleteClient(clientManagerService
				.retrieveClient(clientId));

		return "redirect:/clients.htm";
	}
}
